﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyThuVien.Model;
using QuanLyThuVien.Core;

namespace QuanLyThuVien.DataAccess
{
    class SachAccess
    {
        dbConnection db = new dbConnection();
        public DataTable laytoanbosach()
        {
            return db.executeSelectProcedureQueryNonParameter("select_dausach");
        }

        //---- Tìm kiếm theo mã sách
        public DataView timkiemMasach(string Masach)
        {
            DataView dataView = laytoanbosach().DefaultView;
            dataView.RowFilter = string.Format("Ma_sach like '%{0}%'", Masach);
            return dataView;
        }
        //---- Tìm kiếm theo tên sach
        public DataView timkiemTensach(string Tensach)
        {
            DataView dataView = laytoanbosach().DefaultView;
            dataView.RowFilter = string.Format("Ten_sach like '%{0}%'", Tensach);
            return dataView;
        }
          //---- Tim Học sinh theo ma va tra ve mot bang
          public DataTable timsachtheoma(string masach)
          {
              SqlParameter[] listParam = new SqlParameter[1];
              SqlParameter ma = new SqlParameter("@Ma_sach", SqlDbType.Char, 10);
              ma.Value = masach;
              listParam[0] = ma;
              return db.executeSelectProcedureQuery("timkiem_dausach", listParam);
          }
        public int suasach(Sach s)
        {
            if ((timsachtheoma(s.Ma_sach1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[8];
                SqlParameter masach = new SqlParameter("@Ma_sach", SqlDbType.Char, 10);
                masach.Value = s.Ma_sach1;
                listParam[0] = masach;
                listParam[1] = new SqlParameter("@Ten_sach", s.Ten_sach1);
                listParam[2] = new SqlParameter("@The_loai", s.The_loai1);
                listParam[3] = new SqlParameter("@Tinh_trang", s.Tinh_trang1);
                listParam[4] = new SqlParameter("@So_luong", s.So_luong1);
                listParam[5] = new SqlParameter("@Nha_sanxuat", s.Nha_sanxuat1);
                listParam[6] = new SqlParameter("@Tac_gia", s.Tac_gia1);
                listParam[7] = new SqlParameter("@Nam_SX", s.Nam_SX1);
                db.executeUpdateProcedureQuery("update_dausach", listParam);
                return 1;
            }
        }
        public int themsach(Sach s)
        {
            if ((timsachtheoma(s.Ma_sach1).Rows.Count>0))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[8];
                SqlParameter masach = new SqlParameter("@Ma_sach", SqlDbType.Char, 10);
                masach.Value = s.Ma_sach1;
                listParam[0] = masach;
                listParam[1] = new SqlParameter("@Ten_sach", s.Ten_sach1);
                listParam[2] = new SqlParameter("@The_loai", s.The_loai1);
                listParam[3] = new SqlParameter("@Tinh_trang", s.Tinh_trang1);
                listParam[4] = new SqlParameter("@So_luong", s.So_luong1);
                listParam[5] = new SqlParameter("@Nha_sanxuat", s.Nha_sanxuat1);
                listParam[6] = new SqlParameter("@Tac_gia", s.Tac_gia1);
                listParam[7] = new SqlParameter("@Nam_SX", s.Nam_SX1);
                db.executeInsertProcedureQuery("insert_dausach", listParam);
                return 1;
            }
            
        }
        public int xoasach(Sach s)
        {

            if ((timsachtheoma(s.Ma_sach1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                listParam[0] = new SqlParameter("@Ma_sach", s.Ma_sach1);
                db.executeDeleteProcedureQuery("delete_dausach", listParam);
                return 1;
            }
        }
    }
}
